package edu.dayu;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: dayu
 * @date: 2023-07-16 16:37
 */
public class Activiti7Demo2 {


    /**
     * 流程图部署
     */
    @Test
    public void test2(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("bpmn/meeting2.bpmn20.xml")
                .name("第二个流程")
                .deploy();
        System.out.println(deploy);
    }

    /**
     * 发起流程
     */
    @Test
    public void test3(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String,Object> map = new HashMap<>();
        map.put("assgin1","张三");
        map.put("assgin2","李四");
        // 通过流程定义 id 启动流程 （act_re_prodef 表）
        ProcessInstance processInstance = runtimeService.startProcessInstanceById("meeting2:1:7503",map);
        System.out.println(processInstance);
    }

    /**
     * 代办查询
     */
    @Test
    public void test4(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        // Task 对象对应的是 act_ru_task 的记录。
        List<Task> tasks = taskService.createTaskQuery().taskAssignee("zhangsan").list();
        System.out.println(tasks);// [Task[id=2505, name=人事审批]]
    }

    /**
     * 任务审批
     */
    @Test
    public void test5(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        // 完成任务 通过 taskId
        taskService.complete("2505");
    }
}
